/*
 * @Author: Wangtao
 * @Date: 2022-07-26 14:45:36
 * @LastEditors: Wangtao
 * @LastEditTime: 2022-07-26 15:44:03
 */
type ResizeHandler = (entry: ResizeObserverEntry, observer: ResizeObserver) => void

const map = new WeakMap<Element, Set<ResizeHandler>>()

const observer = new ResizeObserver((entries, ob) => {
  for (let i = 0; i < entries.length; i++) {
    const entry = entries[i], handlers = map.get(entry.target)
    handlers?.forEach(e => e(entry, ob))
  }
})

export default {
  observer (el: Element, handler?: ResizeHandler) {
    if (handler) {
      const handlers = map.get(el)
      if (handlers) {
        handlers.add(handler)
      } else {
        map.set(el, new Set([handler]))
      }
    }
    observer.observe(el)
  },
  dispose (el: Element, handler?: ResizeHandler) {
    if (handler) {
      map.get(el)?.delete(handler)
    } else {
      map.delete(el)
      observer.unobserve(el)
    }
  },
  unobserve (el: Element) {
    observer.unobserve(el)
  }
}